﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Upsam.RopaOnline.Modelo.Common;

namespace Upsam.RopaOnline.Web
{
	public partial class StreetTypeSearch : System.Web.UI.Page
	{
		private const string PAGE_NAME = "StreetTypeSearch.aspx";
		private const string MANAGEMENT_PAGE_NAME = "StreetTypeManagement.aspx";
		
		protected void Page_Load( object sender, EventArgs e )
		{

		}
		
		protected void btnSearch_Click( object sender, EventArgs e )
		{
			// Manda al GridView a resfrescar sus datos contra la base de datos.
			grvStreetType.DataBind();
		}

		protected void btnAdd_Click( object sender, EventArgs e )
		{
			string url = string.Format( CultureInfo.InvariantCulture,
				"{0}?PageMode={1}&IdEntity={2}", MANAGEMENT_PAGE_NAME,
				PageMode.InsertMode, 0 );
			Response.Redirect( url, true );
		}

		protected void grvProductType_RowCommand( object sender, GridViewCommandEventArgs e )
		{
			if( e.CommandName.Equals( ConstantsEnums.EditItem ) )
			{
				GridView currentGrid = sender as GridView;
				int idEntity = 0;
				if( currentGrid != null && e.CommandArgument != null &&
					int.TryParse( currentGrid.Rows[Convert.ToInt32( e.CommandArgument )].Cells[2].Text,
					out idEntity ) )
				{
					string url = string.Format( CultureInfo.InvariantCulture,
						"{0}?PageMode={1}&IdEntity={2}", MANAGEMENT_PAGE_NAME,
						PageMode.EditMode, idEntity );
					Response.Redirect( url, true );
				}
				else
					throw new ArgumentNullException( "Id del registro inválido" );
			}
		}
	}
}
